﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Windows.Forms;
using Nissen_Model;
using Nissen_Resources;
using PosHacker;
using PosHacker.PosException;
using Nissen_Common;
using Utility_Log;


namespace Nissen_Manage.Forms
{
	public partial class TestManage : Form
	{
		private POSHacker vPOSHacker;
		private Dictionary<string, string> vTestNames;

		private bool _closeFlag = false;
		public Form MainForm { get; set; }
		private DataGridViewCheckBoxHeaderCellHelper _checkBoxHeaderCellHelper;
		private IList<string > selectTestsID=new List<string>();
		public TestManage()
		{
			InitializeComponent();
			var currentUser = ContextManager.Instance.CurrentUser;

			this.vPOSHacker = POSHacker.GetInstance(currentUser.NissenSSO.NissenSSOCode, currentUser.NissenSSO.NissenSSOPWD);
			_checkBoxHeaderCellHelper = new DataGridViewCheckBoxHeaderCellHelper(DisplayFlag);
			_checkBoxHeaderCellHelper.AddChekBoxHeaderForGridViewColumn();
		}

		/// <summary>
		/// 加载
		/// </summary>
		private void Init()
		{

			try
			{
				this.vPOSHacker.Login();

				this.GetTestNames();
			}
			catch (PosException e)
			{
				//this.Invoke();
				MessageBox.Show(e.Message);
			}

		}
		/// <summary>
		/// 获取Test数据
		/// </summary>
		private void GetTestNames()
		{

			this.vTestNames =
			this.vPOSHacker.GetTestNames();
			this.Invoke(new PosDelegate(SetTestNames));
		}
		/// <summary>
		/// 设置Test数据
		/// </summary>
		private void SetTestNames()
		{
			
		
			IList<Test> tests = new List<Test>();
			foreach (var vTestName in vTestNames)
			{
				Test test = new Test();
				test.TestId = vTestName.Value;
				test.TestName = vTestName.Key;
				if (selectTestsID.Contains(test.TestId))
				{
					test.DisplayFlag = true;
				}
				
				if (!string.IsNullOrEmpty(vTestName.Value))
				{
					tests.Add(test);
				}
			}

			BindingDgvTest(tests);

			SetLoadingPic(false);
			_closeFlag = false;
		}

		/// <summary>
		/// 数据绑定DGV
		/// </summary>
		/// <param name="tests"></param>
		private void BindingDgvTest(IList<Test> tests)
		{

			this.dgvTest.AutoGenerateColumns = false;
			this.dgvTest.DataSource = tests;
			
		}
		/// <summary>
		/// 数据绑定DGV
		/// </summary>
		/// <param name="tests"></param>
		private void BindingDgvDBTest(IList<Test> tests)
		{

			this.dgvDBTest.AutoGenerateColumns = false;
			this.dgvDBTest.DataSource = tests;

		}

		private void LoadDBData()
		{
			TestService testService = new TestService();
			IList<Test> tests = testService.GetShowTests();
			BindingDgvDBTest(tests);


			foreach (var test in tests)
			{

				selectTestsID.Add(test.TestId);
			}
		}


		private void TestManage_Load(object sender, EventArgs e)
		{
			try
			{
				_closeFlag = true;
				SetLoadingPic(true);
				LoadDBData();
				new Thread(new ThreadStart(Init)).Start();
			}
			catch (Exception)
			{

				MessageBox.Show(ResourceCommon.GetResource("LoadError"));
			}
		

			//TestService testService = new TestService();
			//var tests = testService.GetTests();

			//BindingDgvTest(tests);
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				IList<Test> tests = (IList<Test>)this.dgvTest.DataSource;

				TestService testService = new TestService();
				testService.SaveTests(tests);

				MessageBox.Show(ResourceCommon.GetResource("SaveSuccess"));
				LoadDBData();
			}
			catch (Exception ex)
			{

				MessageBox.Show(ResourceCommon.GetResource("SaveFail"));
				Log4netManager.Instance.Logs.Error("error", ex);
			}
			
		}

		/// <summary>
		/// 弹出新画面
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void dgvTest_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex != -1)
			{
				Test test = (Test)dgvTest.Rows[e.RowIndex].DataBoundItem;
				if (test != null)
				{
					SubjectCourseManage subjectCourseManage = new SubjectCourseManage(test,this.vPOSHacker);
					subjectCourseManage.ShowDialog();
				}
			}
		}

		private void TestManage_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.MainForm.Show();
		}

		private void TestManage_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (_closeFlag)
			{
				e.Cancel = true;
				MessageBox.Show(ResourceCommon.GetResource("LoadingNotClose"));
			}
			
		}

		private void SetLoadingPic(bool flag)
		{
			this.pictureBox1.Visible = flag;
		}
	}
}
